package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.*;
import com.JZ.QM.common.entity.*;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;
import com.JZ.QM.service.ProductEarningRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.fabric.HashShardMapping;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/8 11:43
 */
@Service("productEarningRecord")
public class ProductEarningRecordServiceImpl extends ServiceImpl<ProductEarningRecordDao, ProductEarningRecordEntity> implements ProductEarningRecordService {

    @Resource
    ProductEarningRecordDao productEarningRecordDao;
    @Resource
    ProductLibraryIncomeDao productLibraryIncomeDao;
    @Resource
    ProductEarningDetailDao productEarningDetailDao;
    @Resource
    WealthPositionDao wealthPositionDao;
    @Resource
    PositionDetailDao positionDetailDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<ProductEarningRecordEntity> page = this.page(
                new Query<ProductEarningRecordEntity>().getPage(params),
                new QueryWrapper<ProductEarningRecordEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 计算产品收益记录
     * @return
     */
    @Override
    public Boolean caculateProductEarning() {
        //产品收益条件构造器
        QueryWrapper queryWrapperPro = new QueryWrapper();
        //持仓列表条件构造器
        QueryWrapper queryWrapperPos = new QueryWrapper();
        BigDecimal proEarningMoney=new BigDecimal(0);
        BigDecimal proEarningRadio = new BigDecimal(0);
        BigDecimal dailyProfit = new BigDecimal(0);
        //产品收益的基本信息列表
        ArrayList<ProductEarningDetailEntity> productEarningRecordEntities = new ArrayList<>();
        //产品收益的基本信息
        ProductEarningDetailEntity productEarningDetailEntity = new ProductEarningDetailEntity();
        //用于添加的产品基本信息
        ProductEarningRecordEntity productEarningRecordResult = new ProductEarningRecordEntity();
        WealthPositionEntity wealthPositionEntity = new WealthPositionEntity();
        //定义时间的格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
        //定义日历时间
        Calendar c = Calendar.getInstance();
        //前一天时间
        Date start=new Date();
        //前一天日期格式转换
        String combinationUpdateDate="";

        //取得前一天日期
        c.add(Calendar.DATE, -1);
        start = c.getTime();
        combinationUpdateDate = dateFormat.format(start);
        //获取产品收益的基本信息
        productEarningRecordEntities=productEarningDetailDao.getProductEarningDetail();
        //循环计算收益信息并且更新产品收益表
        for (Iterator<ProductEarningDetailEntity> it = productEarningRecordEntities.iterator(); it.hasNext(); ) {
            productEarningDetailEntity = it.next();
            System.out.println(productEarningDetailEntity);
            queryWrapperPro.select("pro_income_daily_radio").eq("pro_income_ts_code",productEarningDetailEntity.getTsCode());
            proEarningRadio=productLibraryIncomeDao.selectOne(queryWrapperPro).getProIncomeDailyRadio();
            dailyProfit=proEarningRadio.multiply(productEarningDetailEntity.getPositionAmount());
            proEarningMoney=productEarningDetailEntity.getPositionAmount().add(dailyProfit);
            //设置返回的产品收益记录信息
            productEarningRecordResult.setEarningAmount(dailyProfit);
            productEarningRecordResult.setEarningRate(proEarningRadio);
            productEarningRecordResult.setWealthAccountId(productEarningDetailEntity.getWealthAccountId());
            productEarningRecordResult.setPositionId(productEarningDetailEntity.getPositionId());
            productEarningRecordResult.setUserId(productEarningDetailEntity.getUserId());
            productEarningRecordResult.setTradeDate(combinationUpdateDate);
            //将信息插入到产品收益记录中
            System.out.println("已经执行2");
            productEarningRecordDao.insert(productEarningRecordResult);
            //更新产品收益到产品收益之中
            wealthPositionEntity.setPositionAmount(proEarningMoney);
            wealthPositionEntity.setPositionId(productEarningDetailEntity.getPositionId());
            wealthPositionDao.updateById(wealthPositionEntity);
            queryWrapperPro.clear();
        }
        return true;
    }

    /**
     * 获取组合产品持仓的可视化数据
     * @param accountId 财富账号id
     * @return
     */
    @Override
    public ArrayList<HashMap> showProductEarningRecordForChart(Long accountId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //组合产品收益信息列表
        ArrayList<HashMap> productDaily=new ArrayList<>();
        ArrayList<PositionDetailEntity> positionDetailEntities=new ArrayList();
        ArrayList<ProductEarningRecordEntity> productEarningRecordEntities=new ArrayList<>();
        PositionDetailEntity positionDetailEntity=new PositionDetailEntity();
        ProductEarningRecordEntity productEarningRecordEntity=new ProductEarningRecordEntity();
        HashMap<String,Object> productDailyEarningItem=new HashMap();
        BigDecimal[] proDailyEarning;
        String [] proDailyTradeDate;
        int size=0;
        int index=0;

        positionDetailEntities=positionDetailDao.getPositionDetailInfo(accountId);
        for (Iterator<PositionDetailEntity> it = positionDetailEntities.iterator(); it.hasNext(); ) {
            positionDetailEntity = it.next();
            System.out.println("数据位"+positionDetailEntity);
            productDailyEarningItem.put("comBaseName",positionDetailEntity.getComName());
            queryWrapper.select("earning_amount","trade_date")
                    .eq("position_id",positionDetailEntity.getAccountId());
            queryWrapper.orderByAsc("trade_date");
            productEarningRecordEntities= (ArrayList<ProductEarningRecordEntity>) productEarningRecordDao.selectList(queryWrapper);
            size=productEarningRecordEntities.size();
            System.out.println("item为"+productEarningRecordEntities);
            proDailyEarning=new BigDecimal[size];
            proDailyTradeDate=new String[size];
            for (Iterator<ProductEarningRecordEntity> iterator = productEarningRecordEntities.iterator(); iterator.hasNext(); ) {
                productEarningRecordEntity = iterator.next();
                proDailyEarning[index]=productEarningRecordEntity.getEarningAmount();
                proDailyTradeDate[index]=productEarningRecordEntity.getTradeDate();
                System.out.println("yiyunxing");
                index++;
            }
            productDailyEarningItem.put("productDailyEarning",proDailyEarning);
            productDailyEarningItem.put("productDailyTradeDate",proDailyTradeDate);
            productDaily.add(productDailyEarningItem);
            queryWrapper.clear();
            productDailyEarningItem=new HashMap<String,Object>();
            index=0;
        }
        return productDaily;

    }

}
